package com.shujia.flink.window;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

import java.time.Duration;

/**
 * @author shujia
 */
public class Demo4EventTimeWindow {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        /*
java,1686966529000
java,1686966530000
java,1686966531000
java,1686966533000
java,1686966535000
java,1686966537000
java,1686966540000
         */
        DataStreamSource<String> lines = env.socketTextStream("master", 8888);

        //解析数据，取出时间字段
        DataStream<Tuple2<String, Long>> wordAndTs = lines.map(line -> {
            String[] split = line.split(",");
            String word = split[0];
            long ts = Long.parseLong(split[1]);
            return Tuple2.of(word, ts);
        }, Types.TUPLE(Types.STRING, Types.LONG));


        WatermarkStrategy<Tuple2<String, Long>> watermarkStrategy = WatermarkStrategy
                //水位线生产策略
                .<Tuple2<String, Long>>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                //知道时间字段
                .withTimestampAssigner((event, ts) -> event.f1);
        //指定时间字段和水位线
        DataStream<Tuple2<String, Long>> ass = wordAndTs.assignTimestampsAndWatermarks(watermarkStrategy);


        //转换成kv格式
        DataStream<Tuple2<String, Integer>> kvs = ass.map(word -> Tuple2.of(word.f0, 1), Types.TUPLE(Types.STRING, Types.INT));

        KeyedStream<Tuple2<String, Integer>, String> keyBy = kvs.keyBy(kv -> kv.f0);

        /*
         * SlidingEventTimeWindows： 滑动的事件时间窗口
         * SlidingProcessingTimeWindows：滑动的处理时间窗口
         * TumblingEventTimeWindows：滚动的事件时间窗口
         * TumblingProcessingTimeWindows：滚动处理时间窗口
         *
         * 如果使用事件时间窗口，数据中必须有一个时间字段
         */
        keyBy
                .window(SlidingEventTimeWindows.of(Time.seconds(10), Time.seconds(5)))
                .sum(1)
                .print();


        env.execute();
    }
}
